package com.neusoft.movie.v_movies.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.fragment.ActivityFragment;
import com.neusoft.movie.v_movies.fragment.ArticlesFragment;
import com.neusoft.movie.v_movies.fragment.MeFragment;
import com.neusoft.movie.v_movies.fragment.MoviesFragment;
import com.neusoft.movie.v_movies.fragment.NewsFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mTabmovies;
    private LinearLayout mTabnews;
    private LinearLayout mTabarticles;
    private LinearLayout mTabactivity;
    private LinearLayout mTabme;

    private ImageButton mImgmovies;
    private ImageButton mImgnews;
    private ImageButton mImgarticles;
    private ImageButton mImgactivity;
    private ImageButton mImgme;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    private TextView movies;
    private TextView news;
    private TextView articles;
    private TextView activity;
    private TextView me;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();//初始化事件
        setSelect(0);
    }

    private void initEvent() {
        mTabmovies.setOnClickListener(this);
        mTabnews.setOnClickListener(this);
        mTabarticles.setOnClickListener(this);
        mTabactivity.setOnClickListener(this);
        mTabme.setOnClickListener(this);
    }

    private void initView() {
        mTabmovies = (LinearLayout) findViewById(R.id.id_tab_movies);
        mTabnews = (LinearLayout) findViewById(R.id.id_tab_news);
        mTabarticles = (LinearLayout) findViewById(R.id.id_tab_articles);
        mTabactivity = (LinearLayout) findViewById(R.id.id_tab_activity);
        mTabme = (LinearLayout) findViewById(R.id.id_tab_me);

        mImgmovies = (ImageButton) findViewById(R.id.id_tab_movies_img);
        mImgnews = (ImageButton) findViewById(R.id.id_tab_news_img);
        mImgarticles = (ImageButton) findViewById(R.id.id_tab_articles_img);
        mImgactivity = (ImageButton) findViewById(R.id.id_tab_activity_img);
        mImgme = (ImageButton) findViewById(R.id.id_tab_me_img);

        movies = (TextView) findViewById(R.id.movies);
        news = (TextView) findViewById(R.id.news);
        articles = (TextView) findViewById(R.id.articles);
        activity = (TextView) findViewById(R.id.activity);
        me = (TextView) findViewById(R.id.me);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (mTab01 == null) {
                    mTab01 = new MoviesFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                mImgmovies.setImageResource(R.drawable.ic_movie_white);
                movies.setTextColor(Color.parseColor("#ffffff"));
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new NewsFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                mImgnews.setImageResource(R.drawable.ic_news_white);
                news.setTextColor(Color.parseColor("#ffffff"));
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new ArticlesFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                mImgarticles.setImageResource(R.drawable.ic_article_white);
                articles.setTextColor(Color.parseColor("#ffffff"));
                break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new ActivityFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                mImgactivity.setImageResource(R.drawable.ic_activity_white);
                activity.setTextColor(Color.parseColor("#ffffff"));
                break;
            case 4:
                if (mTab05 == null) {
                    mTab05 = new MeFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else {
                    transaction.show(mTab05);
                }
                mImgme.setImageResource(R.drawable.ic_me_white);
                me.setTextColor(Color.parseColor("#ffffff"));
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
            movies.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
            news.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);
            articles.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);
            activity.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab05 != null) {
            transaction.hide(mTab05);
            me.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.id_tab_movies:
                setSelect(0);
                break;
            case R.id.id_tab_news:
                setSelect(1);
                break;
            case R.id.id_tab_articles:
                setSelect(2);
                break;
            case R.id.id_tab_activity:
                setSelect(3);
                break;
            case R.id.id_tab_me:
                setSelect(4);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void resetImgs() {
        mImgmovies.setImageResource(R.drawable.ic_movie_gray);
        mImgnews.setImageResource(R.drawable.ic_news_gray);
        mImgarticles.setImageResource(R.drawable.ic_article_gray);
        mImgactivity.setImageResource(R.drawable.ic_activity_gray);
        mImgme.setImageResource(R.drawable.ic_me);
    }
}
